﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using IPC.Communication.BasicFramework;
using IPC.Communication.Reflection;
using IPC.Communication.Core.Types;

namespace IPC.Communication.Core.Net
{
    public class NetworkDataServerBase : NetworkAuthenticationServerBase, IDisposable, IReadWriteNet
    {
        public delegate void DataReceivedDelegate(object sender, object source, byte[] data);

        public delegate void DataSendDelegate(object sender, byte[] data);

        private List<string> TrustedClients = null;

        private bool IsTrustedClientsOnly = false;

        private SimpleHybirdLock lock_trusted_clients;

        private List<AppSession> listsOnlineClient;

        private object lockOnlineClient;

        private int onlineCount = 0;

        private Timer timerHeart;

        private IByteTransform _byteTransform;
        private string _connectionId;
        private bool _enableWrite = true;
        private TimeSpan _activeTimeSpan;
        private ushort _wordLength = 1;

        public IByteTransform ByteTransform
        {
            get { return _byteTransform; }
            set { _byteTransform = value; }
        }

        public string ConnectionId
        {
            get { return _connectionId; }
            set { _connectionId = value; }
        }

        public bool EnableWrite
        {
            get { return _enableWrite; }
            set { _enableWrite = value; }
        }


        public TimeSpan ActiveTimeSpan
        {
            get { return _activeTimeSpan; }
            set { _activeTimeSpan = value; }
        }

        protected ushort WordLength
        {
            get { return _wordLength; }
            set { _wordLength = value; }
        }


        public int OnlineCount { get { return onlineCount; } set { onlineCount = value; } }

        public AppSession[] GetOnlineSessions
        {
            get
            {
                lock (lockOnlineClient)
                {
                    return listsOnlineClient.ToArray();
                }
            }
        }

        public event DataReceivedDelegate OnDataReceived;

        public event DataSendDelegate OnDataSend;

        public NetworkDataServerBase()
        {
            ActiveTimeSpan = TimeSpan.FromHours(24.0);
            lock_trusted_clients = new SimpleHybirdLock();
            ConnectionId = SoftBasic.GetUniqueStringByGuidAndRandom();
            lockOnlineClient = new object();
            listsOnlineClient = new List<AppSession>();
            timerHeart = new Timer(ThreadTimerHeartCheck, null, 2000, 10000);
        }

        public void SaveDataPool(string path)
        {
            byte[] bytes = SaveToBytes();
            File.WriteAllBytes(path, bytes);
        }

        public void LoadDataPool(string path)
        {
            if (File.Exists(path))
            {
                byte[] content = File.ReadAllBytes(path);
                LoadFromBytes(content);
            }
        }

        protected virtual void LoadFromBytes(byte[] content)
        {
        }

        protected virtual byte[] SaveToBytes()
        {
            return new byte[0];
        }

        protected void RaiseDataReceived(object source, byte[] receive)
        {
            if (this.OnDataReceived != null)
            {
                this.OnDataReceived.Invoke(this, source, receive);
            }
        }

        protected void RaiseDataSend(byte[] send)
        {
            if (this.OnDataSend != null)
            {
                this.OnDataSend.Invoke(this, send);
            }
        }

        protected virtual void ThreadPoolLoginAfterClientCheck(Socket socket, IPEndPoint endPoint)
        {
        }

        //protected override void ThreadPoolLogin(Socket socket, IPEndPoint endPoint)
        //{
        //    string ipAddress = endPoint.Address.ToString();
        //    if (IsTrustedClientsOnly && !CheckIpAddressTrusted(ipAddress))
        //    {
        //        if (base.LogNet != null)
        //        {
        //            base.LogNet.WriteDebug(ToString(), string.Format(StringResources.Language.ClientDisableLogin, endPoint));
        //        }
        //        socket.Close();
        //        return;
        //    }
        //    if (!base.IsUseAccountCertificate)
        //    {
        //        if (base.LogNet != null)
        //        {
        //            base.LogNet.WriteDebug(ToString(), string.Format(StringResources.Language.ClientOnlineInfo, endPoint));
        //        }
        //    }
        //    ThreadPoolLoginAfterClientCheck(socket, endPoint);
        //}

        public void SetTrustedIpAddress(List<string> clients)
        {
            lock_trusted_clients.Enter();
            if (clients != null)
            {
                TrustedClients = clients.Select(delegate(string m)
                {
                    IPAddress iPAddress = IPAddress.Parse(m);
                    return iPAddress.ToString();
                }).ToList();
                IsTrustedClientsOnly = true;
            }
            else
            {
                TrustedClients = new List<string>();
                IsTrustedClientsOnly = false;
            }
            lock_trusted_clients.Leave();
        }

        private bool CheckIpAddressTrusted(string ipAddress)
        {
            if (IsTrustedClientsOnly)
            {
                bool result = false;
                lock_trusted_clients.Enter();
                for (int i = 0; i < TrustedClients.Count; i++)
                {
                    if (TrustedClients[i] == ipAddress)
                    {
                        result = true;
                        break;
                    }
                }
                lock_trusted_clients.Leave();
                return result;
            }
            return false;
        }

        public string[] GetTrustedClients()
        {
            string[] result = new string[0];
            lock_trusted_clients.Enter();
            if (TrustedClients != null)
            {
                result = TrustedClients.ToArray();
            }
            lock_trusted_clients.Leave();
            return result;
        }

        protected void AddClient(AppSession session)
        {
            lock (lockOnlineClient)
            {
                listsOnlineClient.Add(session);
                onlineCount++;
            }
        }

        protected void RemoveClient(AppSession session, string reason = "")
        {
            lock (lockOnlineClient)
            {
                if (listsOnlineClient.Remove(session))
                {
                    if (base.LogNet != null)
                    {
                        base.LogNet.WriteDebug(ToString(), string.Format(StringResources.Language.ClientOfflineInfo, session.IpEndPoint) + " " + reason);
                    }
                    if (session.WorkSocket != null)
                    {
                        session.WorkSocket.Close();
                    }
                    onlineCount--;
                }
            }
        }

        protected override void CloseAction()
        {
            base.CloseAction();
            lock (lockOnlineClient)
            {
                for (int i = 0; i < listsOnlineClient.Count; i++)
                {
                    if (listsOnlineClient[i] != null && listsOnlineClient[i].WorkSocket != null)
                    {
                        listsOnlineClient[i].WorkSocket.Close();
                    }
                    if (base.LogNet != null)
                    {
                        base.LogNet.WriteDebug(ToString(), string.Format(StringResources.Language.ClientOfflineInfo, listsOnlineClient[i].IpEndPoint));
                    }
                }
                listsOnlineClient.Clear();
                onlineCount = 0;
            }
        }

        private void ThreadTimerHeartCheck(object obj)
        {
            AppSession[] array = null;
            lock (lockOnlineClient)
            {
                array = listsOnlineClient.ToArray();
            }
            if (array == null || array.Length == 0)
            {
                return;
            }
            for (int i = 0; i < array.Length; i++)
            {
                if (DateTime.Now - array[i].HeartTime > ActiveTimeSpan)
                {
                    RemoveClient(array[i]);
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (lock_trusted_clients != null)
                {
                    lock_trusted_clients.Dispose();
                }
                this.OnDataSend = null;
                this.OnDataReceived = null;
            }
            base.Dispose(disposing);
        }

        public override string ToString()
        {
            return String.Format("NetworkDataServerBase[{0}]", base.Port);
        }

        [HslMqttApi("BatchRead", "批量读取")]
        public OperateResult<Dictionary<string, object>> BatchRead(Dictionary<string, DataTypeEnum> addresses, int batchNumber)
        {
            return new OperateResult<Dictionary<string, object>>(StringResources.Language.NotSupportedFunction);
        }

        [HslMqttApi("BatchWrite", "批量写入")]
        public OperateResult BatchWrite(Dictionary<string, object> addresses, int batchNumber)
        {
            return new OperateResult(StringResources.Language.NotSupportedFunction);
        }

        [HslMqttApi("ReadByteArray", "")]
        public virtual OperateResult<byte[]> Read(string address, ushort length)
        {
            return new OperateResult<byte[]>(StringResources.Language.NotSupportedFunction);
        }

        [HslMqttApi("WriteByteArray", "")]
        public virtual OperateResult Write(string address, byte[] value)
        {
            return new OperateResult(StringResources.Language.NotSupportedFunction);
        }

        [HslMqttApi("ReadBoolArray", "")]
        public virtual OperateResult<bool[]> ReadBool(string address, ushort length)
        {
            return new OperateResult<bool[]>(StringResources.Language.NotSupportedFunction);
        }

        [HslMqttApi("ReadBool", "")]
        public virtual OperateResult<bool> ReadBool(string address)
        {
            return ByteTransformHelper.GetResultFromArray(ReadBool(address, 1));
        }

        [HslMqttApi("WriteBoolArray", "")]
        public virtual OperateResult Write(string address, bool[] value)
        {
            return new OperateResult(StringResources.Language.NotSupportedFunction);
        }

        [HslMqttApi("WriteBool", "")]
        public virtual OperateResult Write(string address, bool value)
        {
            return Write(address, new bool[1]
		{
			value
		});
        }

        public OperateResult<T> ReadCustomer<T>(string address) where T : IDataTransfer, new()
        {
            OperateResult<T> operateResult = new OperateResult<T>();
            T content = new T();
            OperateResult<byte[]> operateResult2 = Read(address, content.ReadCount);
            if (operateResult2.IsSuccess)
            {
                content.ParseSource(operateResult2.Content);
                operateResult.Content = content;
                operateResult.IsSuccess = true;
            }
            else
            {
                operateResult.ErrorCode = operateResult2.ErrorCode;
                operateResult.Message = operateResult2.Message;
            }
            return operateResult;
        }

        public OperateResult WriteCustomer<T>(string address, T data) where T : IDataTransfer, new()
        {
            return Write(address, data.ToSource());
        }

        public virtual OperateResult<T> Read<T>() where T : class, new()
        {
            return HslReflectionHelper.Read<T>(this);
        }

        public virtual OperateResult Write<T>(T data) where T : class, new()
        {
            return HslReflectionHelper.Write(data, this);
        }

        [HslMqttApi("ReadInt16", "")]
        public OperateResult<short> ReadInt16(string address)
        {
            return ByteTransformHelper.GetResultFromArray(ReadInt16(address, 1));
        }

        [HslMqttApi("ReadInt16Array", "")]
        public virtual OperateResult<short[]> ReadInt16(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * WordLength)), (byte[] m) => ByteTransform.TransInt16(m, 0, length));
        }

        [HslMqttApi("ReadUInt16", "")]
        public OperateResult<ushort> ReadUInt16(string address)
        {
            return ByteTransformHelper.GetResultFromArray(ReadUInt16(address, 1));
        }

        [HslMqttApi("ReadUInt16Array", "")]
        public virtual OperateResult<ushort[]> ReadUInt16(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * WordLength)), (byte[] m) => ByteTransform.TransUInt16(m, 0, length));
        }

        [HslMqttApi("ReadInt32", "")]
        public OperateResult<int> ReadInt32(string address)
        {
            return ByteTransformHelper.GetResultFromArray(ReadInt32(address, 1));
        }

        [HslMqttApi("ReadInt32Array", "")]
        public virtual OperateResult<int[]> ReadInt32(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * WordLength * 2)), (byte[] m) => ByteTransform.TransInt32(m, 0, length));
        }

        [HslMqttApi("ReadUInt32", "")]
        public OperateResult<uint> ReadUInt32(string address)
        {
            return ByteTransformHelper.GetResultFromArray(ReadUInt32(address, 1));
        }

        [HslMqttApi("ReadUInt32Array", "")]
        public virtual OperateResult<uint[]> ReadUInt32(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * WordLength * 2)), (byte[] m) => ByteTransform.TransUInt32(m, 0, length));
        }

        [HslMqttApi("ReadFloat", "")]
        public OperateResult<float> ReadFloat(string address)
        {
            return ByteTransformHelper.GetResultFromArray(ReadFloat(address, 1));
        }

        [HslMqttApi("ReadFloatArray", "")]
        public virtual OperateResult<float[]> ReadFloat(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * WordLength * 2)), (byte[] m) => ByteTransform.TransSingle(m, 0, length));
        }

        [HslMqttApi("ReadInt64", "")]
        public OperateResult<long> ReadInt64(string address)
        {
            return ByteTransformHelper.GetResultFromArray(ReadInt64(address, 1));
        }

        [HslMqttApi("ReadInt64Array", "")]
        public virtual OperateResult<long[]> ReadInt64(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * WordLength * 4)), (byte[] m) => ByteTransform.TransInt64(m, 0, length));
        }

        [HslMqttApi("ReadUInt64", "")]
        public OperateResult<ulong> ReadUInt64(string address)
        {
            return ByteTransformHelper.GetResultFromArray(ReadUInt64(address, 1));
        }

        [HslMqttApi("ReadUInt64Array", "")]
        public virtual OperateResult<ulong[]> ReadUInt64(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * WordLength * 4)), (byte[] m) => ByteTransform.TransUInt64(m, 0, length));
        }

        [HslMqttApi("ReadDouble", "")]
        public OperateResult<double> ReadDouble(string address)
        {
            return ByteTransformHelper.GetResultFromArray(ReadDouble(address, 1));
        }

        [HslMqttApi("ReadDoubleArray", "")]
        public virtual OperateResult<double[]> ReadDouble(string address, ushort length)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, (ushort)(length * WordLength * 4)), (byte[] m) => ByteTransform.TransDouble(m, 0, length));
        }

        [HslMqttApi("ReadString", "")]
        public OperateResult<string> ReadString(string address, ushort length)
        {
            return ReadString(address, length, Encoding.ASCII);
        }

        public virtual OperateResult<string> ReadString(string address, ushort length, Encoding encoding)
        {
            return ByteTransformHelper.GetResultFromBytes(Read(address, length), (byte[] m) => ByteTransform.TransString(m, 0, m.Length, encoding));
        }

        [HslMqttApi("WriteInt16Array", "")]
        public virtual OperateResult Write(string address, short[] values)
        {
            return Write(address, ByteTransform.TransByte(values));
        }

        [HslMqttApi("WriteInt16", "")]
        public virtual OperateResult Write(string address, short value)
        {
            return Write(address, new short[1]
		{
			value
		});
        }

        [HslMqttApi("WriteUInt16Array", "")]
        public virtual OperateResult Write(string address, ushort[] values)
        {
            return Write(address, ByteTransform.TransByte(values));
        }

        [HslMqttApi("WriteUInt16", "")]
        public virtual OperateResult Write(string address, ushort value)
        {
            return Write(address, new ushort[1]
		{
			value
		});
        }

        [HslMqttApi("WriteInt32Array", "")]
        public virtual OperateResult Write(string address, int[] values)
        {
            return Write(address, ByteTransform.TransByte(values));
        }

        [HslMqttApi("WriteInt32", "")]
        public OperateResult Write(string address, int value)
        {
            return Write(address, new int[1]
		{
			value
		});
        }

        [HslMqttApi("WriteUInt32Array", "")]
        public virtual OperateResult Write(string address, uint[] values)
        {
            return Write(address, ByteTransform.TransByte(values));
        }

        [HslMqttApi("WriteUInt32", "")]
        public OperateResult Write(string address, uint value)
        {
            return Write(address, new uint[1]
		{
			value
		});
        }

        [HslMqttApi("WriteFloatArray", "")]
        public virtual OperateResult Write(string address, float[] values)
        {
            return Write(address, ByteTransform.TransByte(values));
        }

        [HslMqttApi("WriteFloat", "")]
        public OperateResult Write(string address, float value)
        {
            return Write(address, new float[1]
		{
			value
		});
        }

        [HslMqttApi("WriteInt64Array", "")]
        public virtual OperateResult Write(string address, long[] values)
        {
            return Write(address, ByteTransform.TransByte(values));
        }

        [HslMqttApi("WriteInt64", "")]
        public OperateResult Write(string address, long value)
        {
            return Write(address, new long[1]
		{
			value
		});
        }

        [HslMqttApi("WriteUInt64Array", "")]
        public virtual OperateResult Write(string address, ulong[] values)
        {
            return Write(address, ByteTransform.TransByte(values));
        }

        [HslMqttApi("WriteUInt64", "")]
        public OperateResult Write(string address, ulong value)
        {
            return Write(address, new ulong[1]
		{
			value
		});
        }

        [HslMqttApi("WriteDoubleArray", "")]
        public virtual OperateResult Write(string address, double[] values)
        {
            return Write(address, ByteTransform.TransByte(values));
        }

        [HslMqttApi("WriteDouble", "")]
        public OperateResult Write(string address, double value)
        {
            return Write(address, new double[1]
		{
			value
		});
        }

        [HslMqttApi("WriteString", "")]
        public virtual OperateResult Write(string address, string value)
        {
            return Write(address, value, Encoding.ASCII);
        }

        public virtual OperateResult Write(string address, string value, int length)
        {
            return Write(address, value, length, Encoding.ASCII);
        }

        public virtual OperateResult Write(string address, string value, Encoding encoding)
        {
            byte[] array = ByteTransform.TransByte(value, encoding);
            if (WordLength == 1)
            {
                array = SoftBasic.ArrayExpandToLengthEven(array);
            }
            return Write(address, array);
        }

        public virtual OperateResult Write(string address, string value, int length, Encoding encoding)
        {
            byte[] data = ByteTransform.TransByte(value, encoding);
            if (WordLength == 1)
            {
                data = SoftBasic.ArrayExpandToLengthEven(data);
            }
            data = SoftBasic.ArrayExpandToLength(data, length);
            return Write(address, data);
        }

        [HslMqttApi("WaitBool", "")]
        public OperateResult<TimeSpan> Wait(string address, bool waitValue, int readInterval = 100, int waitTimeout = -1)
        {
            return ReadWriteNetHelper.Wait(this, address, waitValue, readInterval, waitTimeout);
        }

        [HslMqttApi("WaitInt16", "")]
        public OperateResult<TimeSpan> Wait(string address, short waitValue, int readInterval = 100, int waitTimeout = -1)
        {
            return ReadWriteNetHelper.Wait(this, address, waitValue, readInterval, waitTimeout);
        }

        [HslMqttApi("WaitUInt16", "")]
        public OperateResult<TimeSpan> Wait(string address, ushort waitValue, int readInterval = 100, int waitTimeout = -1)
        {
            return ReadWriteNetHelper.Wait(this, address, waitValue, readInterval, waitTimeout);
        }

        [HslMqttApi("WaitInt32", "")]
        public OperateResult<TimeSpan> Wait(string address, int waitValue, int readInterval = 100, int waitTimeout = -1)
        {
            return ReadWriteNetHelper.Wait(this, address, waitValue, readInterval, waitTimeout);
        }

        [HslMqttApi("WaitUInt32", "")]
        public OperateResult<TimeSpan> Wait(string address, uint waitValue, int readInterval = 100, int waitTimeout = -1)
        {
            return ReadWriteNetHelper.Wait(this, address, waitValue, readInterval, waitTimeout);
        }

        [HslMqttApi("WaitInt64", "")]
        public OperateResult<TimeSpan> Wait(string address, long waitValue, int readInterval = 100, int waitTimeout = -1)
        {
            return ReadWriteNetHelper.Wait(this, address, waitValue, readInterval, waitTimeout);
        }

        [HslMqttApi("WaitUInt64", "")]
        public OperateResult<TimeSpan> Wait(string address, ulong waitValue, int readInterval = 100, int waitTimeout = -1)
        {
            return ReadWriteNetHelper.Wait(this, address, waitValue, readInterval, waitTimeout);
        }

        

        //[AsyncStateMachine(typeof(<WaitAsync>d__112))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<TimeSpan>> WaitAsync(string address, bool waitValue, int readInterval = 100, int waitTimeout = -1)
        //{
        //    <WaitAsync>d__112 stateMachine = new <WaitAsync>d__112();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<TimeSpan>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.waitValue = waitValue;
        //    stateMachine.readInterval = readInterval;
        //    stateMachine.waitTimeout = waitTimeout;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WaitAsync>d__113))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<TimeSpan>> WaitAsync(string address, short waitValue, int readInterval = 100, int waitTimeout = -1)
        //{
        //    <WaitAsync>d__113 stateMachine = new <WaitAsync>d__113();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<TimeSpan>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.waitValue = waitValue;
        //    stateMachine.readInterval = readInterval;
        //    stateMachine.waitTimeout = waitTimeout;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WaitAsync>d__114))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<TimeSpan>> WaitAsync(string address, ushort waitValue, int readInterval = 100, int waitTimeout = -1)
        //{
        //    <WaitAsync>d__114 stateMachine = new <WaitAsync>d__114();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<TimeSpan>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.waitValue = waitValue;
        //    stateMachine.readInterval = readInterval;
        //    stateMachine.waitTimeout = waitTimeout;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WaitAsync>d__115))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<TimeSpan>> WaitAsync(string address, int waitValue, int readInterval = 100, int waitTimeout = -1)
        //{
        //    <WaitAsync>d__115 stateMachine = new <WaitAsync>d__115();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<TimeSpan>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.waitValue = waitValue;
        //    stateMachine.readInterval = readInterval;
        //    stateMachine.waitTimeout = waitTimeout;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WaitAsync>d__116))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<TimeSpan>> WaitAsync(string address, uint waitValue, int readInterval = 100, int waitTimeout = -1)
        //{
        //    <WaitAsync>d__116 stateMachine = new <WaitAsync>d__116();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<TimeSpan>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.waitValue = waitValue;
        //    stateMachine.readInterval = readInterval;
        //    stateMachine.waitTimeout = waitTimeout;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WaitAsync>d__117))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<TimeSpan>> WaitAsync(string address, long waitValue, int readInterval = 100, int waitTimeout = -1)
        //{
        //    <WaitAsync>d__117 stateMachine = new <WaitAsync>d__117();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<TimeSpan>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.waitValue = waitValue;
        //    stateMachine.readInterval = readInterval;
        //    stateMachine.waitTimeout = waitTimeout;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WaitAsync>d__118))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<TimeSpan>> WaitAsync(string address, ulong waitValue, int readInterval = 100, int waitTimeout = -1)
        //{
        //    <WaitAsync>d__118 stateMachine = new <WaitAsync>d__118();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<TimeSpan>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.waitValue = waitValue;
        //    stateMachine.readInterval = readInterval;
        //    stateMachine.waitTimeout = waitTimeout;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadAsync>d__119))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
        //{
        //    <ReadAsync>d__119 stateMachine = new <ReadAsync>d__119();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__120))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, byte[] value)
        //{
        //    <WriteAsync>d__120 stateMachine = new <WriteAsync>d__120();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadBoolAsync>d__121))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult<bool[]>> ReadBoolAsync(string address, ushort length)
        //{
        //    <ReadBoolAsync>d__121 stateMachine = new <ReadBoolAsync>d__121();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadBoolAsync>d__122))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult<bool>> ReadBoolAsync(string address)
        //{
        //    <ReadBoolAsync>d__122 stateMachine = new <ReadBoolAsync>d__122();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__123))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, bool[] value)
        //{
        //    <WriteAsync>d__123 stateMachine = new <WriteAsync>d__123();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__124))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, bool value)
        //{
        //    <WriteAsync>d__124 stateMachine = new <WriteAsync>d__124();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadCustomerAsync>d__125<>))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<T>> ReadCustomerAsync<T>(string address) where T : IDataTransfer, new()
        //{
        //    <ReadCustomerAsync>d__125<T> stateMachine = new <ReadCustomerAsync>d__125<T>();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<T>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteCustomerAsync>d__126<>))]
        //[DebuggerStepThrough]
        //public Task<OperateResult> WriteCustomerAsync<T>(string address, T data) where T : IDataTransfer, new()
        //{
        //    <WriteCustomerAsync>d__126<T> stateMachine = new <WriteCustomerAsync>d__126<T>();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.data = data;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadAsync>d__127<>))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult<T>> ReadAsync<T>() where T : class, new()
        //{
        //    <ReadAsync>d__127<T> stateMachine = new <ReadAsync>d__127<T>();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<T>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__128<>))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync<T>(T data) where T : class, new()
        //{
        //    <WriteAsync>d__128<T> stateMachine = new <WriteAsync>d__128<T>();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.data = data;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadInt16Async>d__129))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<short>> ReadInt16Async(string address)
        //{
        //    <ReadInt16Async>d__129 stateMachine = new <ReadInt16Async>d__129();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<short>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadInt16Async>d__130))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult<short[]>> ReadInt16Async(string address, ushort length)
        //{
        //    <ReadInt16Async>d__130 stateMachine = new <ReadInt16Async>d__130();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<short[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadUInt16Async>d__131))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<ushort>> ReadUInt16Async(string address)
        //{
        //    <ReadUInt16Async>d__131 stateMachine = new <ReadUInt16Async>d__131();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<ushort>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadUInt16Async>d__132))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult<ushort[]>> ReadUInt16Async(string address, ushort length)
        //{
        //    <ReadUInt16Async>d__132 stateMachine = new <ReadUInt16Async>d__132();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<ushort[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadInt32Async>d__133))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<int>> ReadInt32Async(string address)
        //{
        //    <ReadInt32Async>d__133 stateMachine = new <ReadInt32Async>d__133();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<int>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadInt32Async>d__134))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult<int[]>> ReadInt32Async(string address, ushort length)
        //{
        //    <ReadInt32Async>d__134 stateMachine = new <ReadInt32Async>d__134();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<int[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadUInt32Async>d__135))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<uint>> ReadUInt32Async(string address)
        //{
        //    <ReadUInt32Async>d__135 stateMachine = new <ReadUInt32Async>d__135();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<uint>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadUInt32Async>d__136))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult<uint[]>> ReadUInt32Async(string address, ushort length)
        //{
        //    <ReadUInt32Async>d__136 stateMachine = new <ReadUInt32Async>d__136();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<uint[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadFloatAsync>d__137))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<float>> ReadFloatAsync(string address)
        //{
        //    <ReadFloatAsync>d__137 stateMachine = new <ReadFloatAsync>d__137();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<float>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadFloatAsync>d__138))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult<float[]>> ReadFloatAsync(string address, ushort length)
        //{
        //    <ReadFloatAsync>d__138 stateMachine = new <ReadFloatAsync>d__138();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<float[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadInt64Async>d__139))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<long>> ReadInt64Async(string address)
        //{
        //    <ReadInt64Async>d__139 stateMachine = new <ReadInt64Async>d__139();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<long>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadInt64Async>d__140))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult<long[]>> ReadInt64Async(string address, ushort length)
        //{
        //    <ReadInt64Async>d__140 stateMachine = new <ReadInt64Async>d__140();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<long[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadUInt64Async>d__141))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<ulong>> ReadUInt64Async(string address)
        //{
        //    <ReadUInt64Async>d__141 stateMachine = new <ReadUInt64Async>d__141();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<ulong>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadUInt64Async>d__142))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult<ulong[]>> ReadUInt64Async(string address, ushort length)
        //{
        //    <ReadUInt64Async>d__142 stateMachine = new <ReadUInt64Async>d__142();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<ulong[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadDoubleAsync>d__143))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<double>> ReadDoubleAsync(string address)
        //{
        //    <ReadDoubleAsync>d__143 stateMachine = new <ReadDoubleAsync>d__143();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<double>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadDoubleAsync>d__144))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult<double[]>> ReadDoubleAsync(string address, ushort length)
        //{
        //    <ReadDoubleAsync>d__144 stateMachine = new <ReadDoubleAsync>d__144();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<double[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadStringAsync>d__145))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<string>> ReadStringAsync(string address, ushort length)
        //{
        //    <ReadStringAsync>d__145 stateMachine = new <ReadStringAsync>d__145();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadStringAsync>d__146))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult<string>> ReadStringAsync(string address, ushort length, Encoding encoding)
        //{
        //    <ReadStringAsync>d__146 stateMachine = new <ReadStringAsync>d__146();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.length = length;
        //    stateMachine.encoding = encoding;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__147))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, short[] values)
        //{
        //    <WriteAsync>d__147 stateMachine = new <WriteAsync>d__147();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__148))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, short value)
        //{
        //    <WriteAsync>d__148 stateMachine = new <WriteAsync>d__148();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__149))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, ushort[] values)
        //{
        //    <WriteAsync>d__149 stateMachine = new <WriteAsync>d__149();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__150))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, ushort value)
        //{
        //    <WriteAsync>d__150 stateMachine = new <WriteAsync>d__150();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__151))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, int[] values)
        //{
        //    <WriteAsync>d__151 stateMachine = new <WriteAsync>d__151();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__152))]
        //[DebuggerStepThrough]
        //public Task<OperateResult> WriteAsync(string address, int value)
        //{
        //    <WriteAsync>d__152 stateMachine = new <WriteAsync>d__152();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__153))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, uint[] values)
        //{
        //    <WriteAsync>d__153 stateMachine = new <WriteAsync>d__153();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__154))]
        //[DebuggerStepThrough]
        //public Task<OperateResult> WriteAsync(string address, uint value)
        //{
        //    <WriteAsync>d__154 stateMachine = new <WriteAsync>d__154();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__155))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, float[] values)
        //{
        //    <WriteAsync>d__155 stateMachine = new <WriteAsync>d__155();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__156))]
        //[DebuggerStepThrough]
        //public Task<OperateResult> WriteAsync(string address, float value)
        //{
        //    <WriteAsync>d__156 stateMachine = new <WriteAsync>d__156();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__157))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, long[] values)
        //{
        //    <WriteAsync>d__157 stateMachine = new <WriteAsync>d__157();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__158))]
        //[DebuggerStepThrough]
        //public Task<OperateResult> WriteAsync(string address, long value)
        //{
        //    <WriteAsync>d__158 stateMachine = new <WriteAsync>d__158();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__159))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, ulong[] values)
        //{
        //    <WriteAsync>d__159 stateMachine = new <WriteAsync>d__159();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__160))]
        //[DebuggerStepThrough]
        //public Task<OperateResult> WriteAsync(string address, ulong value)
        //{
        //    <WriteAsync>d__160 stateMachine = new <WriteAsync>d__160();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__161))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, double[] values)
        //{
        //    <WriteAsync>d__161 stateMachine = new <WriteAsync>d__161();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.values = values;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__162))]
        //[DebuggerStepThrough]
        //public Task<OperateResult> WriteAsync(string address, double value)
        //{
        //    <WriteAsync>d__162 stateMachine = new <WriteAsync>d__162();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__163))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, string value)
        //{
        //    <WriteAsync>d__163 stateMachine = new <WriteAsync>d__163();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__164))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, string value, Encoding encoding)
        //{
        //    <WriteAsync>d__164 stateMachine = new <WriteAsync>d__164();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.encoding = encoding;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__165))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, string value, int length)
        //{
        //    <WriteAsync>d__165 stateMachine = new <WriteAsync>d__165();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.length = length;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<WriteAsync>d__166))]
        //[DebuggerStepThrough]
        //public virtual Task<OperateResult> WriteAsync(string address, string value, int length, Encoding encoding)
        //{
        //    <WriteAsync>d__166 stateMachine = new <WriteAsync>d__166();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.address = address;
        //    stateMachine.value = value;
        //    stateMachine.length = length;
        //    stateMachine.encoding = encoding;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}
    }
}